package com.bit.cfdi.web.vm.productservices;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.VariableResolver;

import com.bit.cfdi.web.domain.Concepto;
import com.bit.cfdi.web.domain.ConceptoParte;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class PartesViewModel {

	private Logger log = LogManager.getLogger(this.getClass().getName());

	private Concepto cPadre;
	private Concepto parte;
	private List<ConceptoParte> partes;
	private ConceptoParte parteSeleccionada;
	private List<Concepto> conceptos;
	private HashMap<String, Object> conceptoItem;
	private double cantidad;

	@Init
	public void init(@ContextParam(ContextType.EXECUTION) Execution exe) {
		@SuppressWarnings("unchecked")
		Map<String, Object> args = (Map<String, Object>) exe.getArg();
		log.info("INIT");
		initComponents();
		conceptoItem = new HashMap<String, Object>();
		conceptoItem = (HashMap<String, Object>) Executions.getCurrent().getArg();
		cPadre = (Concepto) conceptoItem.get("concepto");
	}

	private void initComponents() {
		partes = new ArrayList<ConceptoParte>();
		cPadre = new Concepto();
		parte = new Concepto();
	}

	@Command
	@NotifyChange("*")
	public void addPartToItem() {
		if (parte != null) {
			ConceptoParte cParte = new ConceptoParte();
			cParte.setCantidad(cantidad);
			cParte.setConcepto(cPadre);
			cParte.setParte(parte);
			partes.add(cParte);
			cantidad = 0;
			parte = new Concepto();
		}
	}

	@Command
	@NotifyChange("visibleWindow")
	public void cancel() {
		partes.clear();
		cantidad = 0;
		parte = new Concepto();
		BindUtils.postGlobalCommand(null, null, 
				"displayProductServicesWindow", null);

	}

	@Command
	@NotifyChange("visibleWindow")
	public void accept() {
		HashMap<String, Object> collection = new HashMap<String, Object>();
		collection.put("partes", partes);
		BindUtils.postGlobalCommand(null, null, "proccessItemParts", collection);
		BindUtils.postGlobalCommand(null, null,
				"displayProductServicesWindow", null);
	}

	private void displayProductServicesWindow() {

	}

	public List<ConceptoParte> getPartes() {
		return partes;
	}

	public void setPartes(List<ConceptoParte> partes) {
		this.partes = partes;
	}

	public Concepto getParte() {
		return parte;
	}

	public void setParte(Concepto parte) {
		this.parte = parte;
	}

	public Concepto getcPadre() {
		return cPadre;
	}

	public double getCantidad() {
		return cantidad;
	}

	public void setCantidad(double cantidad) {
		this.cantidad = cantidad;
	}

	public List<Concepto> getConceptos() {
		return conceptos;
	}

	public void setConceptos(List<Concepto> conceptos) {
		this.conceptos = conceptos;
	}

	public ConceptoParte getParteSeleccionada() {
		return parteSeleccionada;
	}

	public void setParteSeleccionada(ConceptoParte parteSeleccionada) {
		this.parteSeleccionada = parteSeleccionada;
	}

}